
/*!
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included
 in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 
 * @File:       CustomSlider
 * @Abstract:   为Slider添加middleTrack，用于实现音乐或视频播放的buffered进度
 * @History:
 
 -2013-10-20 创建 by xuwf
 */

#import "CustomSlider.h"

/* slider 的高度 */
#define  kPlayProgressBarHeight 1.5


@implementation CustomSlider{
//    UIView *_bgProgressView;         // 背景颜色
//    UIView *_finishPlayProgressView; // 已经播放的进度颜色
    
    CGPoint _lastPoint;
    UIButton *_sliderBtn;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
//        _minimumValue = 0.f;
//        _maximumValue = 1.f;
        
        self.backgroundColor = [UIColor clearColor];
        
        CGFloat showY = 20;
        
//        /* 背景 */
//        _bgProgressView = [[UIView alloc] init];
//        _bgProgressView.backgroundColor = [UIColor greenColor];
//        [self addSubview:_bgProgressView];
//        
//        [_bgProgressView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.mas_left).with.offset(5);
//            make.right.equalTo(self.mas_right).with.offset(-5);
//            make.centerY.equalTo(self.centerY);
//        }];
        
        /* 缓存进度 */
        _ableBufferProgressView = [[UIImageView alloc] initWithFrame:CGRectMake(2, showY, 0, kPlayProgressBarHeight)];
        _ableBufferProgressView.hidden = YES;
        _ableBufferProgressView.image = [IMG(@"white-1.png") stretchableImageWithLeftCapWidth:1 topCapHeight:1];
        [self addSubview:_ableBufferProgressView];
        
//        /* 播放进度 */
//        _finishPlayProgressView = [[UIView alloc] initWithFrame:CGRectMake(0, showY, 0, kPlayProgressBarHeight)];
//        _finishPlayProgressView.backgroundColor = [UIColor redColor];
//        [self addSubview:_finishPlayProgressView];
//        
//        /* 滑动按钮 */
//        _sliderBtn = [[MyProgressSliderBtn alloc] initWithFrame:CGRectMake(0, showY, 44, 44)];
//        CGPoint center = _sliderBtn.center;
//        center.x = _bgProgressView.frame.origin.x;
//        center.y = _finishPlayProgressView.center.y;
//        _sliderBtn.center = center;
//        [_sliderBtn setImage:IMG(@"spot.png") forState:UIControlStateNormal];
//        [_sliderBtn setImage:IMG(@"spot.png") forState:UIControlStateHighlighted];
//
//        [_sliderBtn addTarget:self action:@selector(beiginSliderScrubbing:) forControlEvents:UIControlEventTouchDown];
//        [_sliderBtn addTarget:self action:@selector(endSliderScrubbing:) forControlEvents:UIControlEventTouchCancel];
//        [_sliderBtn addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents:UIControlEventTouchDragInside];
//        [_sliderBtn addTarget:self action:@selector(touchUpInsideSliderScrubbing:) forControlEvents:UIControlEventTouchUpInside];
//        [_sliderBtn addTarget:self action:@selector(touchUpOutsideSliderScrubbing:) forControlEvents:UIControlEventTouchUpOutside];
//        _lastPoint = _sliderBtn.center;
//        [self addSubview:_sliderBtn];
    }
    return self;
}

//- (void)setPlayProgressBackgoundColor:(UIColor *)playProgressBackgoundColor{
//    if (_playProgressBackgoundColor != playProgressBackgoundColor) {
//        _finishPlayProgressView.backgroundColor = playProgressBackgoundColor;
//    }
//    
//}

- (void)setTrackBackgoundColor:(UIColor *)trackBackgoundColor{
    if (_trackBackgoundColor != trackBackgoundColor) {
        _ableBufferProgressView.backgroundColor = trackBackgoundColor;
    }
}

//- (void)setProgressBackgoundColor:(UIColor *)progressBackgoundColor{
//    if (_progressBackgoundColor != progressBackgoundColor) {
//        _bgProgressView.backgroundColor = progressBackgoundColor;
//    }
//}
//
///**
// 进度值
// */
//- (void)setValue:(CGFloat)progressValue{
//    
//    _value = progressValue;
//    
//    float tempValue = _bgProgressView.frame.size.width;
//    
//    NSLog(@"_bgProgressView.frame.size.width is = %f",tempValue);
//    CGFloat finishValue = _bgProgressView.frame.size.width * progressValue;
//    CGPoint tempPoint = _sliderBtn.center;
//    tempPoint.x =  _bgProgressView.frame.origin.x + finishValue;
//    
//    if (tempPoint.x >= _bgProgressView.frame.origin.x &&
//        tempPoint.x <= self.frame.size.width){
//        
//        _sliderBtn.center = tempPoint;
//        _lastPoint = _sliderBtn.center;
//        
//        CGRect tempFrame = _finishPlayProgressView.frame;
//        tempFrame.size.width = tempPoint.x;
//        _finishPlayProgressView.frame = tempFrame;
//    }
//    
//}

/**
 设置缓冲进度值
 */
-(void)setTrackValue:(CGFloat)trackValue{
    _ableBufferProgressView.hidden = NO;
    
    CGFloat finishValue = self.frame.size.width * trackValue - 4;
    if (finishValue > 0) {
        _ableBufferProgressView.frame = CGRectMake(2, 20, finishValue, kPlayProgressBarHeight);
    }else{
        _ableBufferProgressView.frame = CGRectMake(2, 20, 0, kPlayProgressBarHeight);
    }
}

///**
// 拖动值发生改变
// */
//- (void) dragMoving: (UIButton *)btn withEvent:(UIEvent *)event{
//    
//    CGPoint point = [[[event allTouches] anyObject] locationInView:self];
//    CGFloat offsetX = point.x - _lastPoint.x;
//    CGPoint tempPoint = CGPointMake(btn.center.x + offsetX, btn.center.y);
//    
//    // 得到进度值
//    CGFloat progressValue = (tempPoint.x - _bgProgressView.frame.origin.x)*1.0f/_bgProgressView.frame.size.width;
//    [self setValue:progressValue];
//    
//    if (_delegate && [_delegate respondsToSelector:@selector(sliderScrubbing:)]) {
//        [_delegate sliderScrubbing:self];
//    }
//
//}
//// 开始拖动
//- (void)beiginSliderScrubbing:(CustomSlider *)slider{
//    if (_delegate && [_delegate respondsToSelector:@selector(beiginSliderScrubbing:)]) {
//        [_delegate beiginSliderScrubbing:self];
//    }
//
//}
//// 结束拖动
//- (void)endSliderScrubbing:(CustomSlider *)slider{
//    if (_delegate && [_delegate respondsToSelector:@selector(endSliderScrubbing:)]) {
//        [_delegate endSliderScrubbing:self];
//    }
//}
//
//// 结束拖动
//- (void)touchUpInsideSliderScrubbing:(CustomSlider *)slider{
//    if (_delegate && [_delegate respondsToSelector:@selector(touchUpInsideSliderScrubbing:)]) {
//        [_delegate touchUpInsideSliderScrubbing:self];
//    }
//}
//
//
//// 结束拖动
//- (void)touchUpOutsideSliderScrubbing:(CustomSlider *)slider{
//    if (_delegate && [_delegate respondsToSelector:@selector(touchUpOutsideSliderScrubbing:)]) {
//        [_delegate touchUpOutsideSliderScrubbing:self];
//    }
//}

@end


/**
 *  为了让拖动按钮变得更大
 */
//@implementation MyProgressSliderBtn{
//    UIImageView *_iconImageView;
//}
//
//- (instancetype)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        _iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width *0.5,
//                                                                       0.5*(self.frame.size.height),
//                                                                       17, 17)];
////        _iconImageView.backgroundColor = [UIColor whiteColor];
////        _iconImageView.layer.cornerRadius = _iconImageView.frame.size.height*0.5;
////        _iconImageView.layer.masksToBounds = YES;
//        [self addSubview:_iconImageView];
//    }
//    return self;
//}
//
//@end

